package com.brian.app_core_libstarter.application.impl;

import android.os.Build;
import android.util.ArrayMap;

import androidx.annotation.RequiresApi;

import com.brian.app_core_libstarter.application.task.ThreadTask;
import com.brian.app_core_libstarter.application.util.TaskUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: PathFinder
 * @Package: com.brian.app_core_libstarter.application.impl
 * @ClassName: DependsTaskCollect
 * @Description: 处理 依赖任务之间的关系
 * @Author: Brian
 * @CreateDate: 2019/11/24 1:38
 * @UpdateUser: Brian
 * @UpdateDate: 2019/11/24 1:38
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public class DependsTaskCollect {

//    private ArrayMap<Class<? extends ThreadTask>, ArrayList<ThreadTask>> mDependedHashMap = new ArrayMap<>();
    private Map<Class<? extends ThreadTask>, ArrayList<ThreadTask>> mDependedHashMap = new HashMap<>();

    public void collectDepends(ThreadTask threadTask) {
        TaskUtil.checkNull(threadTask);
        if (threadTask.isHasDepends()) {
            // 遍历依赖的task
            for (Class<? extends ThreadTask> cls : threadTask.getDependsClassList()) {
                // 判空 并创建map
                if (mDependedHashMap.get(cls) == null) {
                    mDependedHashMap.put(cls, new ArrayList<ThreadTask>());
                }
                // 将task 添加到map 中
                mDependedHashMap.get(cls).add(threadTask);
            }
        }
    }

    /**
     * 通知Children一个前置任务已完成
     *
     * @param launchTask
     */
    public void satisfyChildren(ThreadTask launchTask) {
        ArrayList<ThreadTask> arrayList = mDependedHashMap.get(launchTask.getClass());
        if (arrayList != null && arrayList.size() > 0) {
            for (ThreadTask task : arrayList) {
                task.satisfy();
            }
        }
    }
}
